/*
 * AIFighter.java
 *
 * Created on 11 November 2006, 11:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package digitrix.fightclub.game;

import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 *
 * @author Josh
 */
public class AIFighter extends Fighter {
    
    private int _agressionPerc;
    private int _movePerc;
    private int _attack1Perc;
    private int _attack2Perc;
    private int _badHealth; // a value which includes when the player should start running away instead of moving towards the player
    
    Random _rand;
    
    /** Creates a new instance of AIFighter */
    public AIFighter( Image img, int width, int height, int playerID ) {
        super( img, width, height, playerID );
        
        _rand = new Random();
    }
    
    public void initActor( int maxHealth, int lives, int xSpeed, int vSpeed, int posX, int posY, boolean facingLeft ) throws Exception {
        super.initActor( maxHealth, lives, xSpeed, vSpeed, posX, posY, facingLeft );
        
        // reset personality
        switch( _playerID ){
            case World.PLAYER_1:
                _agressionPerc = 60;
                _movePerc = 80;
                _attack1Perc = 50;
                _attack2Perc = 80;
                break;
            case World.PLAYER_2:
                _agressionPerc = 50;
                _movePerc = 80;
                _attack1Perc = 40;
                _attack2Perc = 80;
                break;
            case World.PLAYER_3:
                _agressionPerc = 70;
                _movePerc = 90;
                _attack1Perc = 50;
                _attack2Perc = 80;
                break;
            case World.PLAYER_4:
                _agressionPerc = 60;
                _movePerc = 80;
                _attack1Perc = 40;
                _attack2Perc = 80;
                break;
        }
        
        _badHealth = (_maxHealth/4);
    }
    
    public void think(){
        int distance = World.getInstance().getOpponentDistance();
        
        if( !World.getInstance().inDangerZone( distance ) ){
            // move towards player
            if( isFacingLeft() )
                moveLeft();
            else
                moveRight();
        } else{
            int behavior = Math.abs(_rand.nextInt())%100;
            
            if( behavior < _agressionPerc ){
                // chase or attack if close enough
                // if distance is greater than 5? then move towards the opponent
                if( distance > 3 ){
                    // move towards
                    if( isFacingLeft() )
                        moveLeft();
                    else
                        moveRight();
                } else{
                    int stateID = _currentState.getStateID();
                    if( (stateID == STATE_STANDING || stateID == STATE_WALKING) && stateID != STATE_DYING ){
                        // attack
                        int attack = Math.abs(_rand.nextInt())%100;
                        if( attack < 50 ) // attack 1 50% of the time
                            setState( Fighter.STATE_ATTACK1 );
                        else if( attack < 80 ) // attack 2 30% of the time
                            setState( Fighter.STATE_ATTACK2 );
                        else // attack 3 20% of the time
                            setState( Fighter.STATE_ATTACK3 );
                    }
                    
                }
            } else if( behavior < _movePerc ){
                // evade
                
                if( _health < _badHealth ){
                    if( isFacingLeft() )
                        moveRight();
                    else
                        moveLeft();
                } else{
                    if( distance < 20 ){
                        // move towards
                        if( isFacingLeft() )
                            moveLeft();
                        else
                            moveRight();
                    }
                }
            } else{
                // random?
            }
        }
    }
    
    public void takeDamage( int damage ){
        super.takeDamage( damage );
        
        // alter personality depening on playerid, damage and current health
        switch( _playerID ){
            case World.PLAYER_1:
                _agressionPerc -= 2; // run away more often
                _movePerc += 2;
                _attack1Perc -= 3;   // throw more kicks
                break;
            case World.PLAYER_2:
                _agressionPerc += 2; // attack more often
                _movePerc -= 2;
                _attack1Perc -= 5;   // throw less punches and more kicks
                _attack2Perc -= 3;   // use players 'power move' more
                break;
            case World.PLAYER_3:
                _agressionPerc -= 4; // run away more
                _movePerc += 4;
                _attack1Perc -= 6;   // throw more kicks
                break;
            case World.PLAYER_4:
                _agressionPerc += 4; // attack more often
                _movePerc -= 4;
                _attack1Perc -= 7;   // throw less punches and more kicks
                _attack2Perc -= 5;   // use players 'power move' more
                break;
        }
    }
    
}
